<template>
  <div class="login-container">
    <div class="container right-panel-active">
      <!-- 注册框 -->
      <div class="container_from container_signup" v-if="registerFlag">
        <el-form :ref="signFrom" class="from" id="from1" @keyup.enter.native.prevent="loginOrRegister">
          <h2 class="form_title">注册账号</h2>
          <input type="tel" placeholder="请输入邮箱号" v-model.trim="signFrom.email" class="input" auto-complete="on">
          <input type="text" placeholder="请输入账号" v-model.trim="signFrom.username" class="input" auto-complete="on">
          <input :type="passwordType" placeholder="请输入密码" @blur="enableButton" v-model.trim="signFrom.password"
                 class="input" auto-complete="on">
          <span class="show-pwd2" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"/>
        </span>
          <el-button :loading="loginLoading" :disabled="buttonDisabled" class="btn" @click.prevent="loginOrRegister">
            点击注册
          </el-button>
        </el-form>
      </div>

      <!-- 登录框 -->
      <div class="container_from container_signin" v-if="loginFlag">
        <el-form :ref="signFrom" class="from" id="from2" autocomplete="on"
                 @keyup.enter.native.prevent="loginOrRegister">
          <h2 class="form_title">欢迎登录</h2>
          <input type="text" placeholder="请输入邮箱" v-if="clickForgetPwd === false" v-model.trim="signFrom.email" class="input" auto-complete="on">
          <input type="text" placeholder="请输入账号或邮箱" v-if="clickForgetPwd === true" v-model.trim="signFrom.username" class="input" auto-complete="on">
          <input :type="passwordType" placeholder="请输入密码" v-if="clickForgetPwd" v-model.trim="signFrom.password"
                 class="input" auto-complete="on">
          <div class="code_container">
            <input type="text" placeholder="请输入验证码" v-if="clickForgetPwd === false"
                   v-model.trim="checkCodeText" class="input forgetPwd" auto-complete="on"/>

            <a class="hasCode" :style="{pointerEvents: codeDisable}" @click="checkCode" v-if="clickForgetPwd === false">{{codeText }}</a>
          </div>
          <span class="show-pwd" @click="showPwd" v-if="clickForgetPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"/>
        </span>
          <!--<div>-->
          <!--  <span>记住密码</span><input type="checkbox" name="rup" id="rup"/>-->
          <!--  <span>自动登录</span><input type="checkbox" name="autoLogin"/>-->
          <!--</div>-->
          <a class="link" @click="forgetPwd" v-if="clickForgetPwd">验证码登录</a>
          <el-button :loading="loginLoading" class="btn" @click.prevent="loginOrRegister">登录</el-button>
        </el-form>
      </div>

      <!--控制注册与登录移动-->
      <div class="container_overlay" :style="{ transform: signinTranslate}">
        <div class="overlay">
          <div class="overlay_panel" v-if="convertLogin">
            <button class="btn" id="signing" @click="convertLogin" v-if="registerFlag">已有账号，直接登录</button>
          </div>
          <div class="overlay_panel" v-if="loginFlag">
            <button class="btn" id="signup" @click="convertRegister" v-if="loginFlag">没有账号，点击注册</button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import {signup,sendCode,codeLogin} from '@/api/user'

export default {
  name: 'Login',
  data() {
    return {
      checkCodeText: '',
      countDown: false,
      interval: null,
      currentTime: 60,
      codeDisable: 'auto',
      clickForgetPwd: true,
      loginLoading: false,
      buttonDisabled: true,
      registerFlag: true,
      loginFlag: true,
      signinTranslate: 'translateX(0%)',
      passwordType: 'password',
      signFrom: {
        username: '',
        password: '',
        email: '',
        code: ''
      }
    }
  },
  computed:{
     codeText(){
       return  this.countDown ? `${this.currentTime}s后获取` : '获取验证码'
     }
  },
  methods: {
    checkCode(){
      if(!this.signFrom || !/^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(this.signFrom.email)){
        this.$message.error('请输入邮箱')
        return
      }
      //  点击无效
      if(this.countDown){
        return;
      }
      sendCode(this.signFrom.email).then(res => this.$message.success('发送成功'))
      this.countDown = true
      this.interval = setInterval( () =>{
        if(this.currentTime > 1){
          this.currentTime--
        }else{
          clearInterval(this.interval)
          this.interval = null
          this.currentTime = 60
          this.countDown = false
        }
      },1000)
    },
    forgetPwd() {
      this.clickForgetPwd = false
    },
    enableButton() {
      this.buttonDisabled = false
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
    },
    convertLogin() {
      this.signinTranslate = 'translateX(0%)'
      this.registerFlag = false
      this.loginFlag = true
      this.signFrom = {}
    },
    convertRegister() {
      this.signinTranslate = 'translateX(-100%)'
      this.loginFlag = false
      this.registerFlag = true
      this.signFrom = {}
    },
    loginOrRegister() {
      this.loginLoading = true
      if (!this.clickForgetPwd || !this.signFrom.email) {
        this.handleLogin()
      } else {
        this.handleRegister()
      }
    },
    handleRegister() {
      if (!this.signFrom || !/^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(this.signFrom.email)) {
        this.$message.error('请输入正确的邮箱号')
        this.loginLoading = false
        return
      }
      if (!this.signFrom.username || this.signFrom.username.length < 2) {
        this.$message.error('账号需要大于两位')
        this.loginLoading = false
        return
      }
      if (!/^(?=.*\d)(?=.*[a-zA-Z]).{6,}$/.test(this.signFrom.password)) {
        this.$message.error('密码需要包含数字和字母且大于6位！')
        this.loginLoading = false
        return
      }
      signup(this.signFrom).then(res => {
        this.convertLogin()
      })
      this.loginLoading = false
    },
    handleLogin() {
      if(this.clickForgetPwd){
        if (!this.signFrom.username) {
          this.$message.error('请输入账号')
          this.loginLoading = false
          return
        }
        if (!this.signFrom.password) {
          this.$message.error('请输入密码')
          this.loginLoading = false
          return
        }
      }
      this.signFrom.code = this.checkCodeText
      console.log(this.signFrom)
      this.$store.dispatch('user/login',this.signFrom).then(() => {
        this.$router.push({path: '/'})
      })
      this.loginLoading = false
    }
  }
}
</script>
<style scoped>
.code_container {
  display: flex;
}

.hasCode {
  font-weight: bold;
  width: 40%;
  display: inline-block;
  text-align: center;
  vertical-align: center;
  padding-top: 18px;
  padding-left: 5px;
  color: deeppink;
  font-size: 1.0rem;
  text-decoration: none;

}

.show-pwd2 {
  position: absolute;
  right: 80px;
  top: 270px;
  font-size: 16px;
  color: #000000;
  cursor: pointer;
  user-select: none;
}

.show-pwd {
  position: absolute;
  right: 80px;
  top: 220px;
  font-size: 16px;
  color: #000000;
  cursor: pointer;
  user-select: none;
}

.login-container {
  align-items: center;
  background: url("../../assets/bg/bg.gif");
  background-attachment: fixed;
  /* */
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
  display: grid;
  height: 100vh;
  place-items: center;
}

.from_title {
  font-weight: 300;
  margin: 0;
  margin-bottom: 1.25rem;
}

.link {
  color: rgb(189, 90, 238);
  font-size: 0.9rem;
  font-weight: bold;
  margin: 1rem 0 0 0;
  text-decoration: none;
}

/* 注册框 */
.container {
  background-color: #e9e9e9;
  border-radius: 0.7rem;
  box-shadow: 0 0.9rem 1.7rem rgba(0, 0, 0, 0.25);
  height: 420px;
  max-width: 758px;
  overflow: hidden;
  position: relative;
  width: 100%;
}

.container_from {
  height: 100%;
  position: absolute;
  top: 0;
  transition: all 0.6s ease-in-out;
}

/* 登录框 */
.container_signin {
  /*控制登录界面移动 */
  left: 0%;
  width: 50%;
  z-index: 2;
  /*控制移动顺序 */
}


.container .right-panel-active .container_signin {
  transform: translateX(100%);
}

.container_signup {
  left: 0;
  opacity: 0;
  width: 50%;
  z-index: 1;
}

.container.right-panel-active .container_signup {
  animation: show 0.6s;
  opacity: 1;
  transform: translateX(100%);
  z-index: 5;
}

.container_overlay {
  height: 100%;
  Left: 50%;
  overflow: hidden;
  position: absolute;
  top: 0;
  transition: transform 0.6s ease-in-out;
  width: 50%;
  z-index: 50;
}

/* 设置左右透明 移动 */
.container.right-panel-active .container_overlay {
  transform: translateX(-100%);
}

.overlay {
  background-color: #008997;
  /*background: url(home4.jpg);*/
  background-attachment: fixed;
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
  height: 100%;
  left: -100%;
  position: relative;
  transform: translateX(0);
  transition: transform 0.6s ease-in-out;
  width: 200%;
}

/* 左边页面透明 移动 */
.container.right-panel-active .overlay {
  transform: translatex(50%);
}

.overlay_panel {
  align-items: center;
  display: flex;
  flex-direction: Column;
  height: 100%;
  justify-content: center;
  position: absolute;
  text-align: center;
  top: 0;
  transform: translateX(0);
  transition: transform 0.6s ease-in-out;
  width: 50%;
}

/* 设置按钮 */
.btn {
  background-color: skyblue;
  background-image: linear-gradient(to right, skyblue, pink 65%);
  border-radius: 5px;
  margin: 0;
  border: 1px solid skyblue;
  color: #222831;
  cursor: pointer;
  font-size: 0.8rem;
  font-weight: bold;
  letter-spacing: 0.1rem;
  padding: 0.9rem 4rem;
  /* 过渡的时间还有样式 */
  text-transform: uppercase;
  transition: transform 80ms ease-in;
}

form > .btn {
  margin-top: 1.5rem;
}

/* 鼠标放上去让按钮缩小 */
.btn:hover {
  transform: scale(0.95);
}

/* 设置点击没有边框 */
input:focus {
  outline: none;
  border: 1px solid skyblue;
}

/* 登录表单 */
.from {
  background-color: #e9e9e9;
  display: flex;
  align-items: center;
  /*justify-content 用于设置或检索弹性盒子元素在主轴（横轴）方向上的对齐方式。 */
  justify-content: center;
  /*  flex-direction: column; 垂直显示 */
  flex-direction: column;
  padding: 0 4rem;
  height: 100%;
  text-align: center;
}

/* 输入账号密码框样式设置 */
.input {
  background-color: #fff;
  border: 1px solid white;
  padding: 0.9rem 0.9rem;
  margin: 0.5rem 0;
  width: 100%;
  border-radius: 20px;
}

.forgetPwd {
  width: 70%;
  display: inline-block;
  margin: 0 0 0 0;
}

input::-ms-reveal,
input::-ms-clear {
  display: none;
}
</style>
